<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class News extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Default function
     */
    public function index($id = false,$alias=false,$post_id=false)
    {
        
        if(!$id || !$alias) show_404();
        if(!$post_id){
            $posts = _get_posts($id);
            if(!empty($posts)){
            if(sizeof($posts)==1){
            $alias = $this->_global->_unserialize($posts[0]->seo_alias);
            if(!empty($alias[$this->session->userdata('lan')]))
            $alias = $alias[$this->session->userdata('lan')]['alias'];
            else
            $alias="";
            redirect('news/c'.$id.'/'.$alias.'-'.$posts[0]->post_id);
            }}
            else show_404();
        }
        else{
            $posts = _get_posts($id,$post_id);
            $a = $this->_global->_unserialize($posts->seo_alias);
            if($a[$this->session->userdata('lan')]['alias']!=$alias) show_404();
        }
        if(!empty($posts)){
            $this->load->model('categories');
            if($id && $post_id===false)
            {
                $category = $this->categories->getCat(array('cat_id'=>$id));
                $a = $this->_global->_unserialize($category->cat_alias);
                if($a[$this->session->userdata('lan')]['alias']!=$alias) show_404();
                $this->assign('category',$category);
            }
            $this->assign('posts',$posts); 
            $this->assign('cat_id',$id); 
            $this->assign('post_id',$post_id);
            $this->assign('alias',$alias); 
        }else show_404();
    }
    function tag($tagline){
        $this->disable_view();
        echo $tagline;
    }
    function set_language($lang='vn'){
        $this->disable_view();
        $this->session->set_userdata('lan',$lang);
        echo $this->session->userdata('lan');
    } 
    
}